import os

# 读取txt.txt文件中的image文件名
with open('srt_txt/txt.txt', 'r', encoding='utf-8') as f:
    content = f.read()

image_names = []
for line in content.split('\n'):
    if line.startswith('[image'):
        image_name = line.split('[image')[1].split(']')[0]
        image_names.append('image' + image_name + '.jpg')

# 获取images文件夹中所有文件名
folder = 'images/'
all_files = os.listdir(folder)

# 删除不在txt.txt中出现的文件
for filename in all_files:
    if filename not in image_names:
        os.remove(os.path.join(folder, filename))

def count_files(dir_path):
    return len([f for f in os.listdir(dir_path) if os.path.isfile(os.path.join(dir_path, f))])

print('剩余文件数:', count_files(folder))
